﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using OneSoft.EntityCore;
using OneSoft.DataAccess;
//using System.DirectoryServices;
using System.Configuration;
using BySoft.Server.Common;
//using SESS.Server.Business;

using BySoft.Server.Consts;
using BySoft.Server.Common.Entity;
using BySoft.Server.Common.Schema;
using BySoft.Server.DataAccess;
namespace BySoft.Server.Business
{
    public partial class UserLogin
    {
        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="employeeType"></param>
        /// <param name="userId"></param>
        /// <param name="pwd"></param>
        /// <returns></returns>
        public static string Login(ref string userId, string pwd)
        {
            List<SysUserEntity> listUsers = SysUser.GetByCondition("where upper(USERLOGINID)='" + userId.ToUpper() + "' and Status='1'");
            if (listUsers.Count == 0)
            {
                //return false;

                return String.Format(Message.NoExists, "用户");
            }
            SysUserEntity oSysUser = listUsers[0];


            userId = oSysUser.UserID;
            if (CommonMethod.Encrypt(pwd) != oSysUser.Password)
            {
                //return false;
                return Message.UserorPwdError;
            }

            //if (userId != CommonConsts.ADMIN_USER)
            //{

            //    //系统密码必须修改
            //    if (oSysUser.Isdefaultpwd.Equals("1"))
            //    {
            //        //return false;
            //        return Message.PlsChangeSysPwd;
            //    }

            //    //密码有效期:一般用户90天,管理员30天
            //    List<SysUserroleEntity> listUserRoles = SysUserrole.GetByCondition("where RoleID='" + CommonConsts.Admin_Role_ID + "' and UserID='" + oSysUser.Userid + "'");
            //    int nDays = listUserRoles.Count == 0 ? 90 : 30;
            //    if (oSysUser.Passwordchangedate.Value.AddDays(nDays) < DateTime.Now.Date)
            //    {
            //        //return false;
            //        return Message.PwdExpired;
            //    }
            //}

            return "ok";
        }  
      
        /// <summary>
        /// 登录成功后修改相关信息
        /// </summary>
        /// <param name="oSysLoginlog"></param>
        /// <returns></returns>
        public static bool ChangeLoginInfo(SysLoginlogEntity oSysLoginlog)
        {
            bool isReturn = true;
            using (DataAccessBroker broker = DataAccessFactory.Instance())
            {
                try
                {
                    broker.BeginTransaction();
                    UserLoginDA.UpdateLoginDate(oSysLoginlog.UserID, broker);
                    DataAccessCommon.Insert(oSysLoginlog,true, broker);
                    broker.Commit();
                }
                catch (Exception ex)
                {
                    isReturn = false;
                    broker.Rollback();

                    throw ex;
                }
               
            }
            return isReturn;
        }

        /// <summary>
        /// 登录失败后修改相关信息
        /// </summary>
        /// <param name="oSysLoginlog"></param>
        /// <returns></returns>
        public static bool ChangeLoginFailInfo(SysLoginlogEntity oSysLoginlog,SysLoginfaillogEntity oSysLoginfaillog)
        {
            bool isReturn = true;
            using (DataAccessBroker broker = DataAccessFactory.Instance())
            {
                try
                {
                    broker.BeginTransaction();

                    //限制userName字段不能超过20字符
                    if (!string.IsNullOrEmpty(oSysLoginlog.UserName) && oSysLoginlog.UserName.Length > 20)
                        oSysLoginlog.UserName = oSysLoginlog.UserName.Substring(0, 20);
                    DataAccessCommon.Insert(oSysLoginlog,true, broker);

                    DataAccessCommon.Insert(oSysLoginfaillog, true, broker);

                    broker.Commit();
                }
                catch (Exception ex)
                {
                    isReturn = false;
                    broker.Rollback();

                    throw ex;
                }
                
            }
            return isReturn;
        }

        /// <summary>
        /// 登出
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public static int Logout(string userId)
        {
            try
            {
               return UserLoginDA.Logout(userId);
            }
            catch
            {
                throw;
            }
        }

        public static DataTable GetLoginLogList(string loginUser, string startDate,
                string endDate, string ip, string loginResult, int pageIndex, int pageSize, out int count)
        {
            return UserLoginDA.GetLoginLogList(loginUser, startDate, endDate, ip, loginResult, pageIndex, pageSize, out count);

        }
    }
}
